//深拷贝
#include <iostream>
#include <cstring>
using namespace std;

class A
{
    char *p;

public:
    A(){p = new char[100];}
    ~A(){delete [] p;}  
    
    //将拷贝构造函数改造为深拷贝
    A(const A &r);

    //将赋值操作符函数改造为深拷贝
    A &operator=(const A &r);
};

A::A(const A &r)
{
    if(r.p != nullptr)
    {
        p = new char[100];
        memcpy(p,r.p,100);
    }
    else
        p = nullptr;
}

A &A::operator=(const A &r)
{
    if(r.p != nullptr)
    {
        delete [] p;
        p = new char[100];
        memcpy(p,r.p,100);
    }
    else
    {
        delete [] p;
        p = nullptr;
    }
    
    return *this;
}

int main(int argc, char const *argv[])
{
    //堆内存
    A *p1 = new A;
    A *p2 = new A(*p1);//拷贝构造，深拷贝
    A *p3 = new A;
    *p3 = *p1;  //赋值操作，深拷贝

    delete p1;
    delete p2;
    delete p3;


    //栈内存
    A a;
    A b(a);
    A c;
    c = a;

    return 0;
}
